import { getRoutes } from "./routes";

const arrayToTree = (items: any, routeComponents: any) => {
  const result: any = [];
  const itemMap: any = {};
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;

    if (!itemMap[id]) {
      itemMap[id] = {
        children: [],
      };
    }

    itemMap[id] = {
      path: item.path,
      element: routeComponents?.[id],
      children: itemMap?.[id]?.["children"],
    };

    const treeItem = itemMap[id];

    if (pid === 0 || id === 0) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        };
      }
      itemMap[pid].children.push(treeItem);
    }
  }
  return result;
};
export const routes = () => {
  const { routes, routeComponents } = getRoutes();
  const list = arrayToTree(Object.values(routes), routeComponents);
  console.log(Object.values(routes));
  return list;
};
